package com.syn.vr.control;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.ViewGroup;
import android.view.WindowManager;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.ListPopupWindow;

import com.syn.vr.adapter.PopupShortAdapter;
import com.syn.vr.bean.ShortDto;

import java.util.ArrayList;
import java.util.List;

public class ShortPopupWindow extends ListPopupWindow {

    private PopupShortAdapter mAdapter;
    private List<ShortDto> mData;

    public void setData(List<ShortDto> data) {
        mData.clear();
        mData.addAll(data);
        mAdapter.notifyDataSetChanged();
    }

    public ShortPopupWindow(@NonNull Context context) {
        super(context);
        init(context);
    }

    public ShortPopupWindow(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public ShortPopupWindow(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    public ShortPopupWindow(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context);
    }

    @SuppressLint("RestrictedApi")
    private void init(Context context) {
        mData = new ArrayList<>();
        mAdapter = new PopupShortAdapter(context, mData);
        setDropDownGravity(Gravity.START);
        setAdapter(mAdapter);
        setDropDownAlwaysVisible(true);
        setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        setPromptPosition(ListPopupWindow.POSITION_PROMPT_BELOW);
        setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
    }
}
